//
// Main module for the Zen Grids system.
//


// Import the global variables needed by all of Zen Grids.
@import "./variables";
// Import the helper functions.
@import "./functions";
@import "./internal/functions-compass-polyfill";


//
// Apply this to create a grid container element.
// @see http://next.zengrids.com/reference/grids/#zen-grid-container
//
@mixin zen-grid-container(
  $context            : none,
  $gutters            : $zen-gutters,
  $gutter-method      : $zen-gutter-method,
  $direction          : $zen-direction,
  $switch-direction   : $zen-switch-direction
) {

  $dir: $direction;
  @if $switch-direction {
    $dir: zen-direction-switch($dir);
  }

  @if $context == flow-item {
    @warn "zen-grid-container()'s $context cannot be set to: flow-item. It has been set to: flow.";
    $context: flow;
  }
  @else if $context == grid {
    @warn "zen-grid-container()'s $context cannot be set to: grid. It has been set to: grid-item.";
    $context: grid-item;
  }

  // @TODO: This is a pre-IE8 line of code; don't remember why its needed.
  @if zen-support-for(ie, "7") {
    *position: relative;
  }

  // We use the "micro clearfix", instead of Compass' clearfix or pie-clearfix.
  &:before,
  &:after {
    content: "";
    display: table;
  }
  &:after {
    clear: both;
  }
  @if zen-support-for(ie, "7") {
    *zoom: 1;
  }

  // Un-do the gutter padding of the parent grid item.
  @if $context == flow and $gutter-method == padding {
    margin: {
      left: -(zen-half-gutter($gutters, left, $dir));
      right: -(zen-half-gutter($gutters, right, $dir));
    }
  }

  // Prevent any padding from messing up the alignment of the nested grid.
  @if $context == grid-item or $context == flow {
    // Copy of undo-zen-apply-gutter-padding()
    padding: {
      left: 0;
      right: 0;
    }
  }
}

//
// Apply this to each grid item. @see http://next.zengrids.com/reference/grids/#zen-grid-item
//
@mixin zen-grid-item(
  $column-span,
  $column-position,
  $direction          : $zen-direction,
  $columns            : $zen-columns,
  $gutters            : $zen-gutters,
  $gutter-method      : $zen-gutter-method,
  $grid-width         : $zen-grid-width,
  $box-sizing         : $zen-box-sizing,
  $switch-direction   : $zen-switch-direction,
  $include-base       : $zen-auto-include-grid-item-base
) {

  // Calculate the unit width.
  $unit-width: zen-unit-width($columns, $gutters, $gutter-method, $grid-width);

  // Calculate the item's width.
  $width: zen-grid-item-width($column-span, $columns, $gutters, $gutter-method, $grid-width, $box-sizing);

  // Calculate the margin from the container's edge.
  $margin: ($column-position - 1) * $unit-width;
  @if $gutter-method == margin {
    $margin: $margin + (floor($column-position) - 1) * $gutters;
  }

  // Determine the float direction and its reverse.
  $dir: $direction;
  @if $switch-direction {
    $dir: zen-direction-switch($dir);
  }
  $rev: zen-direction-switch($dir);

  float: $dir;
  width: $width;
  margin: {
    #{$dir}: $margin;
    #{$rev}: -100%;
  }
  @if zen-support-for(ie, "7") {
    *margin-#{$rev}: -99.9%;
  }

  // Include the grid item base mixin.
  @if $include-base {
    @include zen-grid-item-base($gutters, $gutter-method, $box-sizing, $direction, $switch-direction);
  }
  // If the $gutters parameter has been used, set the gutters even if
  // $include-base is false.
  @else if $gutters != $zen-gutters and $gutter-method == padding {
    @include zen-apply-gutter-padding($gutters, $direction, $switch-direction);
  }
}

//
// Applies a standard set of properites for grid items in the layout.
// @see http://next.zengrids.com/reference/grids/#zen-grid-item-base
//
@mixin zen-grid-item-base(
  $gutters            : $zen-gutters,
  $gutter-method      : $zen-gutter-method,
  $box-sizing         : $zen-box-sizing,
  $direction          : $zen-direction,
  $switch-direction   : $zen-switch-direction
) {

  // Specify the padding if the gutter method uses padding.
  @if $gutter-method == padding {
    @include zen-apply-gutter-padding($gutters, $direction, $switch-direction);
  }

  // Specify the border-box properties.
  @if $box-sizing == border-box {
    -moz-box-sizing: border-box;
    -webkit-box-sizing: border-box;
    -ms-box-sizing: border-box;
    box-sizing: border-box;
  }
  // Prevent left/right borders since they'll break the layout with content-box.
  @else if $box-sizing == content-box {
    border: {
      left: 0 !important;
      right: 0 !important;
    }
    @if $gutter-method == margin {
      padding: {
        left: 0 !important;
        right: 0 !important;
      }
    }
  }

  @if zen-support-for(ie, "7") {
    @if $box-sizing == border-box and $box-sizing-polyfill-path == "" {
      @warn "Setting $box-sizing to #{$box-sizing} will fail for legacy IE browsers because the $box-sizing-polyfill-path is empty.";
    }
    @if $box-sizing-polyfill-path != "" {
      *behavior: url($box-sizing-polyfill-path);
    }
    @if zen-support-for(ie, "6") {
      // Display inline or double your floated margin!
      // @see http://www.positioniseverything.net/explorer/doubled-margin.html
      _display: inline;
      // Prevent overflowing content from breaking the layout.
      _overflow: hidden;
      // In IE6, overflow visible is broken.
      // @see http://www.howtocreate.co.uk/wrongWithIE/?chapter=overflow%3Avisible%3B
      _overflow-y: visible;
    }
    // Prevent overflowing content from being hidden beneath other grid items.
    *word-wrap: break-word;
  }
}

//
// Apply this to a grid item so that it starts a new row.
// @see http://next.zengrids.com/reference/grids/#zen-new-row
//
@mixin zen-new-row(
  $clear              : $zen-direction,
  $switch-direction   : $zen-switch-direction
) {
  // Determine the clearing direction.
  @if $switch-direction {
    $clear: zen-direction-switch($clear);
  }
  clear: $clear;
}

//
// Applies the gutter to a grid item when using the padding gutter method.
// @see http://next.zengrids.com/reference/grids/#zen-apply-gutter-padding
//
@mixin zen-apply-gutter-padding(
  $gutters            : $zen-gutters,
  $direction          : $zen-direction,
  $switch-direction   : $zen-switch-direction
) {

  $dir: $direction;
  @if $switch-direction {
    $dir: zen-direction-switch($dir);
  }

  padding: {
    left: zen-half-gutter($gutters, left, $dir);
    right: zen-half-gutter($gutters, right, $dir);
  }
}
